﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UtilityLibrary;
using Enemies;
using MapLibrary.Sound;


namespace MapLibrary.Players.Skills
{
    class Laser : Skill
    {
        public const float LASER_CHUNK_LENGTH = 400.0f; //in Pixels
        private LevelDepValue<float> laserLength;              //in Chunks

        private LevelDepValue<int> coolDown;        //How long is cooldown?
        private int coolDownCount;          //How long have we cooleddown so far?

        protected LevelDepValue<int> activatableTime; //How long can we keep the laser on?
        private int activatableTimeCount;   //How long have we been firing continuously?
        private bool activatable;           //Have we waited through cooldown since last fire?
        private bool activated;             //In the last update, were we firing?

        LevelDepValue<float> damage;        //How much damage will this do on impact?
                                            //NOTE: Should be VERY small!

        private double growthRate = .01;

        private LaserInstance currentInstance;
        public Boolean hitSurface { get; set; }
        public Laser()
            : base()
        {
            setLevelThresholds();
            coolDownCount = 0;
            activatableTimeCount = 0;
            activatable = false;
            activated = false;
            hitSurface = false;
        }

        public override void Update()
        {
            if (!activatable)
                UpdateCoolDownCount();
            else if (activated)
            {
                activatableTimeCount++;

                if (activatableTimeCount > activatableTime.Value)
                {
                    activated = false;
                    activatable = false;
                    activatableTimeCount = 0;
                }
            }
        }

        public double portionOfChunk()
        {
            double growth = growthRate * activatableTimeCount;
            if (growth >= 1)
                return 1;
            else
                return growth;
        }

        public void Deactivate()
        {
            //coolDownCount = 0;
            activatableTimeCount = 0;
            activatable = false;
            activated = false;
            currentInstance = null;

        }

        public override void Activate(Player p)
        {
            if (activatable)
            {
                if (!activated)
                {
                    SoundManager.GetSoundManager().SoundBank.PlayCue("nanobot_laser");
                    currentInstance = p.World.ProjectileManager.AddLaserInstance(p) as LaserInstance;
                    currentInstance.laser = this;
                    currentInstance.TimeToLive = activatableTime.Value;
                    currentInstance.maxLength = laserLength.Value * LASER_CHUNK_LENGTH;
                    activated = true;
                    activatableTimeCount = 0;
                }
                /*else
                    activatableTimeCount++;

                if (activatableTimeCount > activatableTime.Value)
                {
                    activated = false;
                    activatable = false;
                    activatableTimeCount = 0;
                }*/
            }
        }

        /*private void drawLaser(Player p)
        {
            p.World.ProjectileManager.DrawLaser(p);
        }*/

        public override bool ActivateConditionsSatisfied(Player p)
        {
            return activatable;
        }

        public void Cancel()
        {
            if (currentInstance != null)
            {
                currentInstance.Remove();
            }
            Deactivate();
        }

        private void UpdateCoolDownCount()
        {
            coolDownCount++;

            if (coolDownCount > coolDown.Value)
            {
                coolDownCount = 0;
                activatable = true;
            }
        }

        private void setLevelThresholds()
        {
            laserLength = new LevelDepValue<float>(this);
            laserLength.AddValueForLevel(1, 2);
            laserLength.AddValueForLevel(2, 4);

            coolDown = new LevelDepValue<int>(this);
            coolDown.AddValueForLevel(1, 60);//TEMPORARILY CHANGED FROM 600
            coolDown.AddValueForLevel(3, 420);

            damage = new LevelDepValue<float>(this);
            damage.AddValueForLevel(1, 0.01f);
            damage.AddValueForLevel(4, 0.03f);

            activatableTime = new LevelDepValue<int>(this);
            activatableTime.AddValueForLevel(1, 500);
            activatableTime.AddValueForLevel(2, 1000);
        }

        public override void DrawAtPlayer(SpriteBatch batch, WorldViewport viewport, Player p)
        {
            Vector2 position = viewport.WorldToViewportCoords(p.Position);
            position = position - p.GetCenter() - new Vector2(0, 60);

            if (!activatable)
                PrimitiveBrush.RenderText(batch, "Laser Cool Down: " + coolDownCount/60, position);
            else if (!activated)
                PrimitiveBrush.RenderText(batch, "Laser Available", position);
            else
                PrimitiveBrush.RenderText(batch, "Laser Activitable Time: " + activatableTimeCount, position);
        }

        //TODO: CHECK THESE!
        public override int GetMax(string trackerName)
        {
            return coolDown.Value;
        }

        public override int GetAmount(string trackerName)
        {
            if (activatable && coolDown.Value == 0)
                return coolDownCount;
            return coolDown.Value;
        }
    }
}
